/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | 弹窗封装方法
 * +----------------------------------------------------------------------
 */
layui.define(['layer', 'configure', 'moduleApi'], function (exports) {
    var layer = layui.layer;
    var configure = layui.configure;
    var moduleApi = layui.moduleApi;
    var $ = layui.$;
    var layerPop = {
        param: {
            area:['900px', '630px'],//默认大小
            index: 0, //弹窗的index
            title: '', //弹窗title
            url: '',    //弹窗引用url
            btns: [],   //弹窗按钮数组
            isTop: false,    //弹窗是否最外层打开
            saveForm: null,    //保存回调函数
            curIframe: null//当前IFRAM窗口对象
        },
        close: function () {
            if (layerPop.param.isTop) {
                top.layer.close(layerPop.param.index);
            } else {
                layer.close(layerPop.param.index);
            }
        },
        open: function () {
            return new Promise(function (reslove) {
                var btnLength = layerPop.param.btns.length;
                layerPop.param.url = moduleApi.BASE_API + layerPop.param.url;
                switch (btnLength) {
                    case 0:
                        layerPop.popupNoBtn().then(function (res) {
                            reslove(res);
                        });
                        break;
                    case 1:
                        layerPop.popupOneBtn().then(function (res) {
                            reslove(res);
                        });
                        break;
                    case 2:
                        layerPop.popupDoubleBtn().then(function (res) {
                            reslove(res);
                        });
                        break;
                    case 3:
                        layerPop.popupThreeBtn().then(function (res) {
                            reslove(res);
                        });
                        break;
                }
            });
        },
        //无按钮弹窗
        popupNoBtn: function () {
            var area = layerPop.param.area || configure.POPUP_AREA_MIDDLE;    //宽高  默认值
            return new Promise(function (reslove) {
                if (layerPop.param.isTop) {
                    layerPop.param.index = top.layer.open({
                        maxmin:true,
                        type: 2,
                        title: layerPop.param.title,
                        skin: 'layui-layer-rim', //加上边框
                        area: area, //宽高
                        content: layerPop.param.url,
                        success: function (layero, index) {
                            var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            iframeWin.parWin = window;
                            iframeWin.parWin.saveForm = layerPop.param.saveForm;
                        },
                        //关闭回调
                        end: function (index, layero) {
                            top.layer.close(layerPop.param.index);
                            return false;
                        },
                        btnAlign: 'c'
                    });
                } else {
                    layerPop.param.index = layer.open({
                        maxmin:true,
                        type: 2,
                        title: layerPop.param.title,
                        skin: 'layui-layer-rim', //加上边框
                        area: area, //宽高
                        content: layerPop.param.url,
                        //关闭回调
                        end: function (index, layero) {
                            layer.close(layerPop.param.index);
                            reslove();
                            return false;
                        },
                        btnAlign: 'c'
                    });
                }
            });
        },
        //单个按钮弹窗
        popupOneBtn: function () {
            var area = layerPop.param.area || configure.POPUP_AREA_MIDDLE;    //宽高  默认值
            return new Promise(function (reslove) {
                if (layerPop.param.isTop) {
                    layerPop.param.index = top.layer.open({
                        maxmin:true,
                        type: 2,
                        title: layerPop.param.title,
                        skin: 'layui-layer-rim', //加上边框
                        area: area, //宽高
                        content: layerPop.param.url,
                        btn: layerPop.param.btns,
                        yes: function (index, layero) {
                            reslove({code: 1, msg: layerPop.param.btns[0], result: {}});
                        },
                        //关闭回调
                        end: function (index, layero) {
                            top.layer.close(layerPop.param.index);
                            reslove({code: 0, msg: '关闭', result: {}});
                            return false;
                        },
                        btnAlign: 'c'
                    });
                } else {
                    layerPop.param.index = layer.open({
                        maxmin:true,
                        type: 2,
                        title: layerPop.param.title,
                        skin: 'layui-layer-rim', //加上边框
                        area: area, //宽高
                        content: layerPop.param.url,
                        btn: layerPop.param.btns,
                        yes: function (index, layero) {
                            reslove({code: 1, msg: layerPop.param.btns[0], result: {}});
                        },
                        //关闭回调
                        end: function (index, layero) {
                            layer.close(layerPop.param.index);
                            reslove({code: 0, msg: '关闭', result: {}});
                            return false;
                        },
                        btnAlign: 'c'
                    });
                }
            });
        },
        //双按钮弹窗
        popupDoubleBtn: function () {
            var area = layerPop.param.area || configure.POPUP_AREA_MIDDLE;    //宽高  默认值
            return new Promise(function (reslove) {
                if (layerPop.param.isTop) {
                    layerPop.param.index = top.layer.open({
                        maxmin:true,
                        type: 2,
                        title: layerPop.param.title,
                        skin: 'layui-layer-rim', //加上边框
                        area: area, //宽高
                        content: layerPop.param.url,
                        btn: layerPop.param.btns,
                        yes: function (index, layero) {
                            $(layerPop.param.curIframe.document).find('#btnSubmit').click();
                            reslove({code: 1, msg: layerPop.param.btns[0], result: {}});
                        },
                        success: function (layero, index) {
                            var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            iframeWin.parWin = window;
                            iframeWin.parWin.saveForm = layerPop.param.saveForm;
                            layerPop.param.curIframe = iframeWin;
                        },
                        btn2: function (index, layero) {
                            reslove({code: 2, msg: layerPop.param.btns[0], result: {}});
                            return false;
                        },
                        //关闭回调
                        end: function (index, layero) {
                            top.layer.close(layerPop.param.index);
                            reslove({code: 0, msg: '关闭', result: {}});
                            return false;
                        },
                        btnAlign: 'c'
                    });
                } else {
                    layerPop.param.index = layer.open({
                        maxmin:true,
                        type: 2,
                        title: layerPop.param.title,
                        skin: 'layui-layer-rim', //加上边框
                        area: area, //宽高
                        content: layerPop.param.url,
                        btn: layerPop.param.btns,
                        yes: function (index, layero) {
                            reslove({code: 1, msg: layerPop.param.btns[0], result: {}});
                        },
                        btn2: function (index, layero) {
                            reslove({code: 2, msg: layerPop.param.btns[0], result: {}});
                            return false;
                        },
                        //关闭回调
                        end: function (index, layero) {
                            layer.close(layerPop.param.index);
                            reslove({code: 0, msg: '关闭', result: {}});
                            return false;
                        },
                        btnAlign: 'c'
                    });
                }
            });
        },
        //三按钮弹窗
        popupThreeBtn: function () {
            var area = layerPop.param.area || configure.POPUP_AREA_MIDDLE;    //宽高  默认值
            return new Promise(function (reslove) {
                if (layerPop.param.isTop) {
                    layerPop.param.index = top.layer.open({
                        maxmin:true,
                        type: 2,
                        title: layerPop.param.title,
                        skin: 'layui-layer-rim', //加上边框
                        area: area, //宽高
                        content: layerPop.param.url,
                        btn: layerPop.param.btns,
                        yes: function (index, layero) {
                            reslove({code: 1, msg: layerPop.param.btns[0], result: {}});
                        },
                        btn2: function (index, layero) {
                            reslove({code: 2, msg: layerPop.param.btns[0], result: {}});
                            return false;
                        },
                        btn3: function (index, layero) {
                            reslove({code: 3, msg: layerPop.param.btns[0], result: {}});
                            return false;
                        },
                        //关闭回调
                        end: function (index, layero) {
                            top.layer.close(layerPop.param.index);
                            reslove({code: 0, msg: '关闭', result: {}});
                            return false;
                        },
                        btnAlign: 'c'
                    });
                } else {
                    layerPop.param.index = layer.open({
                        maxmin:true,
                        type: 2,
                        title: layerPop.param.title,
                        skin: 'layui-layer-rim', //加上边框
                        area: area, //宽高
                        content: layerPop.param.url,
                        btn: layerPop.param.btns,
                        yes: function (index, layero) {
                            reslove({code: 1, msg: layerPop.param.btns[0], result: {}});
                        },
                        btn2: function (index, layero) {
                            reslove({code: 2, msg: layerPop.param.btns[0], result: {}});
                            return false;
                        },
                        btn3: function (index, layero) {
                            reslove({code: 3, msg: layerPop.param.btns[0], result: {}});
                            return false;
                        },
                        //关闭回调
                        end: function (index, layero) {
                            layer.close(layerPop.param.index);
                            reslove({code: 0, msg: '关闭', result: {}});
                            return false;
                        },
                        btnAlign: 'c'
                    });
                }
            });
        },
        //Top单按钮窗口弹出
        popOneTopFrame: function () {
            var area = layerPop.param.area || configure.POPUP_AREA_MIDDLE;    //宽高  默认值
            layerPop.param.index = top.layer.open({
                maxmin:true,
                type: 2,
                title: layerPop.param.title,
                skin: 'layui-layer-rim', //加上边框
                area: area, //宽高
                content: layerPop.param.url,
                btn: layerPop.param.btns,
                yes: function (index, layero) {
                    var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    $(iframeWin.document).find('#btnSubmit').click();
                },
                success: function (layero, index) {
                    var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    iframeWin.parWin = window;
                    iframeWin.parWin.saveForm = layerPop.param.saveForm;
                },
                btnAlign: 'c'
            });
        }
    };
    exports('layerPop', layerPop);
});